<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
  <link link href="_/css/grids-min.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <link link href="_/css/v3.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <link link href="_/css/downloads.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <link href='https://fonts.googleapis.com/css?family=Exo:700,700italic|Average' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="_/css/mystyle.css">
  <script type="text/javascript" src="_/js/jquery-1.8.2.min.js"></script>
  <script type="text/javascript" src="_/js/myscript.js"></script>
<?php 
	require_once("database.php");
	require_once("facebook_include1.php");
	?>


<?php
    require_once("./config.php");
    $me = $facebook->api('/me');
    $access_token = $facebook->getAccessToken();
    $user = $facebook->getUser();
	
	
?>

<?php
	login($access_token);
	$userId = get_uid_only($access_token);
	$conn = connectdb();
	$userName = getUserName($userId);
        $useremail = get_email($user_id);
	closedb($conn);
	$receiverid = getReceiverId($userId);
	$conn = connectdb();
	$moneySent = $_GET['moneySent'];
        $receiverid = getReceiverId($userId);
	updateBalance($userId, $receiverid, $moneySent, True);
	
	//TODO : has to be modified so that the user gets 8 rounds
	changeState($userId, $receiverid, true);
	$receiverMail = getEmail($receiverid);
	$gameDetails = getPairDetails($userId, $receiverid);
	$relation = $gameDetails['relation'];
	$anonymous = $gameDetails['anonymous'];
	$senderBalance = getBalance($userId);
	
	if($moneySent != 0){
		$msg = '<a href="http://www.facebook.com/profile.php?id='.$userId.'">'. getName($userId) . '</a> sent you some cash through the Investment game. <a href="http://apps.facebook.com/trustgame/selected.php">Please click here to proceed further.</a> Good luck again and have fun!';
		$msg = '';
		if($anonymous){
			if($relation == 1 || $relation == 4){
				$msg = 'Sender sent you some cash through the Investment game. <a href="http://apps.facebook.com/trustgame/selected.php">Please click here to proceed further.</a> Good luck again and have fun!';
			}else if($relation == 2 || $relation == 3){
				$msg = 'Your anonymous friend sent you some cash through the Investment game. <a href="http://apps.facebook.com/trustgame/selected.php">Please click here to proceed further.</a> Good luck again and have fun!';	
			}
		}else{
			if($relation == 1 || $relation == 4){
				$msg = '<a href="http://www.facebook.com/profile.php?id='.$userId.'">'. getName($userId) . '</a> sent you some cash through the Investment game. <a href="http://apps.facebook.com/trustgame/selected.php">Please click here to proceed further.</a> Good luck again and have fun!';
			}else if($relation == 2 || $relation == 3){
				$msg = '<a href="http://www.facebook.com/profile.php?id='.$userId.'">'. getName($userId) . '</a> sent you some cash through the Investment game. <a href="http://apps.facebook.com/trustgame/selected.php">Please click here to proceed further.</a> Good luck again and have fun!';	
			}
		}
		$email = new Mail_Postmark();
		$email->to($receiverMail, 'Investment Game Administrator')
	   	 ->subject('The sender has responded')
	    	->messageHtml($msg)
	    	->send();
		$conn = connectdb();
		logEvent($userId, $receiverid, $moneySent, 5);
		closedb($conn);	 
	}else{
		$msg = '<a href="http://www.facebook.com/profile.php?id='.$userId.'">'. getName($userId) . '</a> dint send you any money in the Investment game. ' .
			'You wont be able to play further.<br/><br/> Good luck and we thank you for playing!';
		$msg = '';
		if($anonymous){
			if($relation == 1 || $relation == 4){
				$msg = 'Sender dint send you any money in the Investment game. ' .
						'You wont be able to play further.<br/><br/> Good luck and we thank you for playing!';
			}else if($relation == 2 || $relation == 3){
				$msg = 'Your anonymous friend dint send you any money in the Investment game. ' .
							'You wont be able to play further.<br/><br/> Good luck and we thank you for playing!';	
			}
		}else{
			if($relation == 1 || $relation == 4){
				$msg = '<a href="http://www.facebook.com/profile.php?id='.$userId.'">'. getName($userId) . '</a> dint send you any money in the Investment game. ' .
							'You wont be able to play further.<br/><br/> Good luck and we thank you for playing!';
			}else if($relation == 2 || $relation == 3){
				$msg = '<a href="http://www.facebook.com/profile.php?id='.$userId.'">'. getName($userId) . '</a> dint send you any money in the Investment game. ' .
								'You wont be able to play further.<br/><br/> Good luck and we thank you for playing!';	
			}
		}
		$email = new Mail_Postmark();
		$email->to($receiverMail, 'Investment Game Administrator')
		    ->subject('The sender has responded')
		    ->messageHtml($msg)
		    ->send();
		$conn = connectdb();
		logEvent($userId, $receiverid, $moneySent, 5);
		closedb($conn);
		$msg = 'Since you dint send any money to the receiver the game ends here. You have ' .  currency.$senderBalance  . 'in your account. The experimenters will email you with more information about your payment.';
		$email = new Mail_Postmark();
		$email->to($useremail, 'Investment Game Administrator')
		    ->subject('Thanks for playing the Investment Game')
		    ->messageHtml($msg)
		    ->send();
		changeState($userId, $receiverid, FALSE);
	}
?>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--block_head-->
<link href="App_Themes/Intel.com/grids-min.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<link href="App_Themes/Intel.com/v3.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<!--object-specific stylesheets-->
<link href="App_Themes/Intel.com/downloads.css" rel="stylesheet" type="text/css">
<script language="javascript" src="scripts/downloads.js" type="text/javascript" xmlns:msxsl="urn:schemas-microsoft-com:xslt"></script>
<!--/object-specific stylesheets-->
<!--/block_head-->
</head>
<body>
<div id="doc" style="width:700px; padding-left:0px; margin-left:0px"> 
	   <div id="bd">
	   		<div id="yui-main"> 
				<div class="yui-b">
					<div class="yui-g">
						<div class="pageHeading" style="padding:15px 10px 15px 0px;">Done !!</div>
						<div style="padding:0px 0px 15px 0px;">
							<p class="font4">You just sent <?php echo currency?><?php echo $moneySent?>. </p>
						</div>
						
						<div id="questions">
							<div class="sectionHeading">You are almost done.<br/> To finish the game and become eligible for your reward please fill out the survey by clicking on Next:</div>
							<form id = "survey" name = "survey" action="survey.php" method="get" class="font4">
								<div class="yui-gc">
									<div class="yui-u first">
										&nbsp;
									</div>
									<div class="yui-u">
										&nbsp;
									</div>
									<div class="yui-u">
										<input type="submit" name="submitButton" id="submitButton" value="Next &gt;" class="search" style="margin-left:-6.5px" /><br />
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
	   		</div> 
	   </div>  
</div> 
</body>

</html>
